/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package top.dzygod.modules.oa.service.impl;

import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.dzygod.modules.oa.domain.Group;
import top.dzygod.modules.oa.repository.GroupRepository;
import top.dzygod.modules.oa.service.GroupService;
import top.dzygod.modules.oa.service.dto.GroupDto;
import top.dzygod.modules.oa.service.dto.GroupQueryCriteria;
import top.dzygod.modules.oa.service.mapstruct.GroupMapper;
import top.dzygod.utils.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

/**
* @website https://dzygod.top
* @description 服务实现
* @author dzyGod
* @date 2020-06-17
**/
@Service
@RequiredArgsConstructor
public class GroupServiceImpl implements GroupService {

    private final GroupRepository groupRepository;
    private final GroupMapper groupMapper;

    @Override
    public Map<String,Object> queryAll(GroupQueryCriteria criteria, Pageable pageable){
        Page<Group> page = groupRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(groupMapper::toDto));
    }

    @Override
    public List<GroupDto> queryAll(GroupQueryCriteria criteria){
        return groupMapper.toDto(groupRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public GroupDto findById(Long id) {
        Group group = groupRepository.findById(id).orElseGet(Group::new);
        ValidationUtil.isNull(group.getId(),"Group","id",id);
        return groupMapper.toDto(group);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public GroupDto create(Group resources) {
        return groupMapper.toDto(groupRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Group resources) {
        Group group = groupRepository.findById(resources.getId()).orElseGet(Group::new);
        ValidationUtil.isNull( group.getId(),"Group","id",resources.getId());
        group.copy(resources);
        groupRepository.save(group);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            groupRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<GroupDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (GroupDto group : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("名称", group.getGroupName());
            map.put("创建时间", group.getGmtCreate());
            map.put("修改时间", group.getGmtModified());
            map.put("激活状态", group.getEnabled());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public Map<String, Object> queryGroupsByUserId(Long userId) {
        final HashMap<String, Object> map = new HashMap<>();
        map.put("content", groupMapper.toDto(groupRepository.findAllByUserId(userId)));
        return map;
    }
}
